// noinspection JSAnnotator
let $;
var define = layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate'], function (exports) {
    $ = layui.$;
    let layer = layui.layer;
    let table = layui.table;
    let form = layui.form;
    var laydate = layui.laydate;
    zmnLayUtil.layFormVerify(form);
    let cols = [
        [{field: 'batchId', type: 'checkbox', fixed: 'left', align: 'center'},
            {field: 'number', title: '序号', align: 'center', type: 'numbers'},
            {field: 'batchId', title: '批次ID', align: 'center'},
            {field: 'batchName', title: '批次名称', align: 'center'},
            {
                field: 'batchName', title: '优惠类型', align: 'center', templet: function (row) {
                    var value = "";
                    if (row.preferentialType && row.preferentialType == 1) {
                        value = "折扣";
                    }
                    if (row.preferentialType && row.preferentialType == 2) {
                        value = "立减";
                    }

                    return value;
                }
            },
            {
                field: 'preferentialValue', title: '面值', align: 'center', templet: function (row) {
                    var value = "";
                    if (row.preferentialValue) {
                        if (row.preferentialType && row.preferentialType == 1) {
                            value = row.preferentialValue / 10 + "折";
                        }
                        if (row.preferentialType && row.preferentialType == 2) {
                            value = (row.preferentialValue / 100) + "元";
                        }
                    }
                    return value;
                }
            },
            {
                field: 'createTime', title: '创建时间', align: 'center', templet: function (row) {
                    return zmnUtil.createDate(row.createTime);
                }
            }
        ]
    ];

    /**
     * 描述: 时间初始化
     **/
    laydate.render({
        elem: '#act-coup-createDate',
    });


    form.render();
    //条件选择查询
    form.on('submit(query-submit)', function (obj) {
        tableRender(obj.field);
        return false;
    });

    let tableRender = function (data) {
        zmnLayUtil.layTableRender(table, cols, "zmn-coupquery-table", data, "/activity/config/getactcouplist.action", {
            limit: 50,
            page: true
        });
    };
    //加载数据
    tableRender({"activityId": json.activityId});

    // 选择（全选或单选）
    var ids = new Array();
    table.on("checkbox(zmn-coupquery-table)", function () {
        ids = zmnPage.layIdsChecked(json.items, "batchId");
    });


    //新增到清单
    $('body').on('click', '#addCoupBatchBtn', function () {
        var batchIdArr = new Array();
        var checkData = table.checkStatus('zmn-coupquery-table').data;
        // alert(JSON.stringify(checkData));
        $.each(checkData, function (key, val) {
            var batchId = val.batchId;
            batchIdArr.push(batchId);
        });

        var param = {"activityId": json.activityId, "batchIds": batchIdArr.toString()};

        $.ajax({
            type: "POST",
            url: "/activity/config/addcoupbatchs",
            dataType: "json",
            data: param,
            success: function (json) {
                if (json.status == 200) {
                    //
                    //layer.msg(json.message);
                   // layer.msg(json.message);
                 //  zmnLayUtil.closeThisIframeMsg(parent, res.message, "zmn-coupquery-table");
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                }else{
                    layer.msg(json.message);
                }

            }
        });


    });


    //对外暴露的接口
    exports('zmn_plat_activity_coupInfo_add', {});
});
